package com.starbaba.log4j.view;

import org.apache.commons.lang3.StringUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.log4j.model.Log4jDetailBean;
import com.starbaba.log4j.service.Log4jStatisticsService;
import com.starbaba.log4j.service.impl.LogStatisticsServiceImpl;
import com.tuanzi.server.loan_base.util.SendEmail;

/**
 * 错误日志详情Action
 * @author chenweijun
 *
 */
public class LogDetailAction extends BaseAction implements ModelDriven<Log4jDetailBean> {

	private static final long serialVersionUID = 1L;
	private Log4jDetailBean model;
	private Log4jStatisticsService service; 
	
	public LogDetailAction(){
		service = new LogStatisticsServiceImpl();
	}
	@Override
	public Log4jDetailBean getModel() {
		if(model == null){
			model = new Log4jDetailBean();
		}
		return model;
	}
	
	/**
	 * 查询错误日志详情
	 */
	public String getLog4jDetail(){
		parsePageRequest();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		model =service.getLog4fDetail(paramsMap);
		paramsMap.put("model", model);
		return SUCCESS;
	}
	
	/**
	 * 发送邮件
	 * @return
	 */
	public String sendEmail(){
		parsePageRequest();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		String projectname = (String) paramsMap.get("projectname");
		String msg = (String) paramsMap.get("msg");
		String stacktrace = (String) paramsMap.get("stacktrace");
		String receiverAddress = (String) paramsMap.get("receiver_address");  //收件人地址
		String cc = "server@xmiles.cn";   //抄送地址
		
		String title = projectname + "异常信息";
		StringBuilder content = new StringBuilder("项目名称：" + projectname);
		
		if(StringUtils.isNotEmpty(msg)){
			content.append("<br/><br/>接口信息：<br/>" + msg.trim());
		}
		if(StringUtils.isNotEmpty(stacktrace)){
			content.append("<br/><br/>错误信息：<br/>" + stacktrace.trim());
		}
		SendEmail.sendEmail(receiverAddress, cc, title, content.toString());
		return SUCCESS;
	}

}
